package com.pi2e.tools;

import java.net.ConnectException;
import java.util.List;

import com.pi2e.common.Pi2eLogger;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**  
* @ClassName: SystemUtil  
* @Description: TODO(系统工具类)  
* @author luotao  
* @date 2016年10月9日 下午4:35:35  
*
 */
public class SystemUtil {
	/** 
	 * 判断相对应的APP是否存在 
	 *  
	 * @param context 
	 * @param packageName(包名)(若想判断QQ，则改为com.tencent.mobileqq，若想判断微信，则改为com.tencent.mm) 
	 * @return 
	 */  
	public static boolean  isPackageAvilible(Context context, String packageName) {  
	    PackageManager packageManager = context.getPackageManager();  
	  
	    //获取手机系统的所有APP包名，然后进行一一比较  
	    List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);  
	    for (int i = 0; i < pinfo.size(); i++) { 
	        if (((PackageInfo) pinfo.get(i)).packageName  
	                .equalsIgnoreCase(packageName))  
	        	
	            return true;  
	    }  
	    return false;  
	}  
	
	public static boolean isNetAvilible(Context context){
		// 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
		ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		// 获取NetworkInfo对象
		NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
		for (int j = 0; j < networkInfo.length; j++) {
			if (networkInfo[j].getState() == NetworkInfo.State.CONNECTED)
			{
			      return true;
			      
			}
		}
		
		return false ;
	}
	

}
